/**
  mensei-system: An inventory and point-of-sale system for Mensei Enterprise
  Copyright (C) 2011 Aldo Balagulan, Patrick Calulo, Wyn Manlim

  This file is part of mensei-system.

  mensei-system is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  mensei-system is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with mensei-system.  If not, see <http://www.gnu.org/licenses/>.
  */

/*
  ShinyList code is based on code from:
  http://qt-articles.blogspot.com/2010/07/how-to-customize-listview-in-qt-using.html

  Coding style modified, and made more object-oriented
 */
#include "shinylistdelegate.h"

#include <QApplication>
#include <QPainter>
#include <QDebug>

ShinyListDelegate::ShinyListDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{

}

QSize ShinyListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QIcon icon = qvariant_cast<QIcon>(index.data(IconRole));
    QSize iconSize = icon.actualSize(option.decorationSize);
    iconSize.setWidth(32);
    iconSize.setHeight(28);

    QFont font = QApplication::font();
    QFontMetrics fm(font);

    return QSize(iconSize.width(), iconSize.height() + fm.height() + 8);
}

void ShinyListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyledItemDelegate::paint(painter, option, index);

    painter->save();

    QFont mainFont = QApplication::font();
    QFont subFont = QApplication::font();

    mainFont.setBold(true);
    mainFont.setPointSize(mainFont.pointSize() + 1);

    subFont.setWeight(subFont.weight() - 2);

    QFontMetrics fm(mainFont);

    QIcon icon = qvariant_cast<QIcon>(index.data(IconRole));
    QString headerText = qvariant_cast<QString>(index.data(HeaderRole));
    QString subText = qvariant_cast<QString>(index.data(SubHeaderRole));

    QSize iconsize = icon.actualSize(option.decorationSize);

    QRect headerRect = option.rect;
    QRect subHeaderRect = option.rect;
    QRect iconRect = subHeaderRect;

    iconRect.setRight(iconsize.width() + 30);
    iconRect.setTop(iconRect.top() + 5);

    // Put a gap between the icon and the labels
    headerRect.setLeft(iconRect.right() + 5);
    subHeaderRect.setLeft(iconRect.right() + 5);

    headerRect.setTop(headerRect.top() + 5);
    // This call to setBottom() appears to limit the QLabel's height
    headerRect.setBottom(headerRect.top() + fm.height());
    subHeaderRect.setTop(headerRect.bottom() + 2);

    painter->drawPixmap(
                QPoint(iconRect.left() + iconsize.width() / 2 + 2,
                       iconRect.top() + iconsize.height() / 2 + 3),
                icon.pixmap(iconsize.width(),iconsize.height())
                );

    painter->setFont(mainFont);
    painter->drawText(headerRect, headerText);

    painter->setFont(subFont);
    // 17 in the second arg pushes the subtext to the lower part of the list item
    painter->drawText(subHeaderRect.left(), subHeaderRect.top() + 17, subText);

    painter->restore();
}
